/*
 *hashmap 1.0.0
 * description:this haspmap is a data structure which is used in the js file and it can be store data whatever you want.
 *
 */
var common=function(){

    // 判断两个对象是否相等
    this.isObjectValueEqual=function (a, b) {
        var aProps = Object.getOwnPropertyNames(a);
        var bProps = Object.getOwnPropertyNames(b);
        if (aProps.length != bProps.length) {
            return false;
        }
        for (var i = 0; i < aProps.length; i++) {
            var propName = aProps[i];
            if (a[propName] !== b[propName]) {
                return false;
            }
        }
        return true;
    }

    /*
     * @description:弱判断两个对象是否相等（忽略属性的先后顺序，只要值相等 @example:A.x=1,A.y=2与A.x=2,A.y=1视为相等
     *
     */
    this.isObjectValueEqualIgnoreSequence=function (a, b) {
        var flag = true;
        var aProps = Object.getOwnPropertyNames(a);
        var bProps = Object.getOwnPropertyNames(b);
        if (aProps.length != bProps.length) {
            return false;
        }
        for (var i = 0; i < aProps.length; i++) {
            if (!isPropertyInObject(b, a[aProps[i]])) {
                flag = false;
            }
        }
        return flag;
    }

    // 判断一个属性值是不是在一个对象里
    this.isPropertyInObject=function (object, value) {
        for ( var i in object) {
            if (object[i] == value) {
                return true;
            }
        }
        return false;
    }

    // 判断一个属性值是不是与一个对象的指定属性值相等
    this.isPropertyValueInAndEqualObjectPropertyValue=function (object, prop, value) {
        if (prop in object) {
            if (object[prop] == value) {
                return true;
            }
        }
        return false;
    }

    // 根据一个属性值找对象中另外一个属性值
    this.FindPropertyValueInObjectWithOtherPropertyValue=function (object, prop1,
                                                                   prop2, value) {
        if (prop1 in object) {
            if (object[prop1] == value) {
                return object[prop2];
            }
        }
        return false;
    }
}

var HashMap=function(){

    var size = 0;// Map大小
    var entry = new Object();// 对象

    this.common=new common(); //取得common的通用方法

    // Map的存put方法
    this.put = function(key, value) {
        if (!this.containsKey(key)) {
            size++;
            entry[key] = value;
        }
    }

    // Map取get方法
    this.get = function(key) {
        return this.containsKey(key) ? entry[key] : null;
    }

    // Map删除remove
    this.remove = function(key) {
        if (this.containsKey(key) && (delete entry[key])) {
            size--;
        }
    }

    // 是否包含Key
    this.containsKey = function(key) {
        return (key in entry);
    }
    // 是否包含Value
    this.containsValue = function(value) {
        for ( var prop in entry) {
            if (isObjectValueEqual(entry[prop], value)) {
                return true;
            }
        }
        return false;
    }

    // 所有的Value
    this.values = function() {
        var values = new Array();
        for ( var prop in entry) {
            values.push(entry[prop]);
        }
        return values;
    }

    // 所有的 Key
    this.keys = function() {
        var keys = new Array();
        for ( var prop in entry) {
            keys.push(prop);
        }
        return keys;
    }

    // Map size
    this.size = function() {
        return size;
    }

    // 清空Map
    this.clear = function() {
        size = 0;
        entry = new Object();
    }

    // 获取key By value
    this.getKeyByValue = function(value) {
        for ( var prop in entry) {
            if (this.common.isObjectValueEqual(entry[prop], value)) {
                console.log("getKeyByValue is ok");
                return prop;
            }
        }
        return null;
    }

    // 包含特定字段对象的所有keys
    this.specialKeys = function(containsID) {
        var keys = new Array();
        var object_container = new Array();
        var values = this.values();
        for (var i = 0; i < values.length; i++) {
            console.log(this.common.isPropertyInObject(values[i], containsID));
            if (this.common.isPropertyInObject(values[i], containsID)) {
                object_container.push(values[i]);
            }
        }
        console.log("object_container.length", object_container.length);
        for (var j = 0; j < object_container.length; j++) {
            keys.push(this.getKeyByValue(object_container[j]));
        }
        return keys;
    }

    // 查找一个弱对象是否存在于哈希表中
    this.findWeekObjectInHash = function(obj) {
        var values = this.values();
        for (var i = 0; i < values.length; i++) {
            if (this.common.isObjectValueEqualIgnoreSequence(obj, values[i])) {
                return true;
            }
        }
        return false;
    }

    // 包含对象特定字段的所有keys
    this.specialKeysSequence = function(containsID, start_end) {
        var keys = new Array();
        var object_container = new Array();
        var values = this.values();
        for (var i = 0; i < values.length; i++) {
            if (this.common.isPropertyValueInAndEqualObjectPropertyValue(values[i],
                start_end, containsID)) {
                object_container.push(values[i]);
            }
        }
        for (var j = 0; j < object_container.length; j++) {
            keys.push(this.getKeyByValue(object_container[j]));
        }
        return keys;
    }

    // 根据对象中是否包含某一属性值去查找hashmap的该value（即该对象）
    this.findObjectByPropety = function(ID) {
        var value = this.values();
        for (var i = 0; i < value.length; i++) {
            if (this.common.isPropertyInObject(value[i], ID)) {
                return value[i];
            }
        }
        return null;
    }
}

export default HashMap;